package com.jishucai.rpc.register;

import com.jishucai.constant.ZKConstant;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

public class ZkRegisterCenter implements RegisterCenter {

    private CuratorFramework curator;

    public ZkRegisterCenter() {
        curator = CuratorFrameworkFactory.builder()
                .connectString("zkOS:2181")
                .connectionTimeoutMs(10000)
                .sessionTimeoutMs(4000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 10)).build();
        curator.start();
    }

    @Override
    public void register(String serviceName, String serviceAddress) throws Exception {
        // 创建持久节点
        // 要创建的服务名称的节点
        String servicePath = ZKConstant.ZK_DUBBO_ROOT_PATH + "/" + serviceName;
        if (curator.checkExists().forPath(servicePath) == null) {
            curator.create().creatingParentsIfNeeded()
                    .withMode(CreateMode.PERSISTENT)
                    .forPath(servicePath);
        }

        // 创建临时节点
        String providerPath = servicePath + "/" + serviceAddress;
        if (curator.checkExists().forPath(providerPath) == null) {
            curator.create().withMode(CreateMode.EPHEMERAL)
                    .forPath(providerPath);
        }

    }

}
